<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="70%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <Tabs v-model:activeKey="activeKey" tabPosition="left">
      <Tabs.TabPane
        v-for="(item, index) in formTabs"
        :key="index"
        :forceRender="true"
        :tab="item.tabName"
      >
        <SingleForm
          v-if="flag"
          :schemas="item.colList"
          :formConfig="formConfig"
          :data="record"
          ref="singleFormArr"
        />
      </Tabs.TabPane>
    </Tabs>
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import { ref, computed } from 'vue';
  import { Tabs } from 'ant-design-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';

  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { formSet } from '/@/api/test/testData';

  import { queslibPlanConfirm } from '/@/api/quesLib/plan/queslibPlan';
  import { queslibResConfirm } from '/@/api/quesLib/res/queslibRes';

  import SingleForm from './singleForm.vue';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('quesLib.res');
  const { showMessage } = useMessage();
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: `${openType.value === 'plan' ? '方案' : '结果'}${ope.value == '3' ? '打回' : '确认'}`,
  }));

  const singleFormArr = ref<any>(null);
  const record = ref<any>({});
  let formTabs = ref<any>([]);
  let formConfig = ref<any>({});

  let flag = ref(false);

  const activeKey = ref<number>(0);

  const openType = ref('');
  const quesId = ref('');
  const ope = ref('');
  let id = '';

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    console.log(data);
    flag.value = false;
    activeKey.value = 0;
    openType.value = data.type;
    quesId.value = data.quesId;
    ope.value = data.ope;
    id = data.id;
    record.value.remarks = '';
    setDrawerProps({ loading: true });

    const res11 = await formSet({ viewCode: data.configName });

    formTabs.value = res11.layVoucherView.formTabs;
    formConfig.value = res11.layVoucherView.formConfig;

    flag.value = true;
    record.value.ts = data.ts;

    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    let obj: any = {};
    let falg = false;
    await singleFormArr.value.forEach((item) => {
      item.validateForm().then((e) => {
        if (!e) {
          falg = true;
        } else {
          obj = { ...obj, ...e };
        }
      });
    });
    setTimeout(async () => {
      if (falg) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      } else {
        try {
          console.log(obj);
          obj.quesId = quesId.value;
          obj.ope = ope.value;
          obj.ts = record.value.ts;
          setDrawerProps({ confirmLoading: true });
          const params: any = {
            id: id,
          };
          //   data.testDataChildList = await getTestDataChildList();
          const res =
            'plan' === openType.value
              ? await queslibPlanConfirm(params, obj)
              : await queslibResConfirm(params, obj); //需修改
          showMessage(res.message);
          setDrawerProps({ confirmLoading: false });
          setTimeout(closeDrawer);
          emit('success', obj);
        } catch (error: any) {
          console.log('error', error);
        } finally {
          setDrawerProps({ confirmLoading: false });
        }
      }
    }, 0);
  }
</script>
